import {
    setToken,
    removeToken
} from '@/util/auth'
import {
    setStore,
    getStore
} from '@/util/store'
import {
    isURL
} from '@/util/validate'
import {
    // encryption,
    deepClone
} from '@/util/util'
import webiste from '@/const/website'
import {
    loginByUsername,
    getUserInfo,
    RefeshToken
} from '@/api/user'
import routerData from "@/router/views/main";


function addPath(ele, first) {
    const propsConfig = webiste.menu.props;
    const propsDefault = {
        label: propsConfig.label || 'label',
        path: propsConfig.path || 'path',
        icon: propsConfig.icon || 'icon',
        children: propsConfig.children || 'children'
    }
    const isChild = ele[propsDefault.children] && ele[propsDefault.children].length !== 0;
    if (!isChild && first) {
        ele[propsDefault.path] = ele[propsDefault.path] + '/index'
        return
    }
    ele[propsDefault.children].forEach(child => {
        if (!isURL(child[propsDefault.path])) {
            child[propsDefault.path] = `${ele[propsDefault.path]}/${child[propsDefault.path]?child[propsDefault.path]:'index'}`
        }
        addPath(child);
    })

}

function calcMenus(menus, permissions, state) {
    const results = [];
    for (let i = 0; i < menus.length; i++) {
        const menu = menus[i];
        if (permissions[menu.name]) {
            results.push(menu);
            if (menu.children && menu.children.length) {
                menu.children = calcMenus(menu.children, permissions, state)
            }
        }
    }
    return results;
}

function calcMenuNames(menus) {
    for (let i = 0; i < menus.length; i++) {
        const menu = menus[i];
        // menu.name = prefix + menu.path;
        if ((menu.children && menu.children.length) || (menu.buttons && menu.buttons.length)) {
            user.state.allPermission[menu.name] = false; //非子节点;
        } else {
            user.state.allPermission[menu.name] = true; //子节点;
        }
        if (menu.children && menu.children.length) {
            calcMenuNames(menu.children)
        } else if (menu.buttons && menu.buttons.length) {
            calcMenuNames(menu.buttons)
        }
    }
}
// function calcMenuNames(prefix, menus) {
//     for (let i = 0; i < menus.length; i++) {
//         const menu = menus[i];
//         // menu.name = prefix + menu.path;
//         if ((menu.children && menu.children.length) || (menu.buttons && menu.buttons.length)) {
//             user.state.allPermission[menu.name] = false; //非子节点;
//         } else {
//             user.state.allPermission[menu.name] = true; //子节点;
//         }
//         if (menu.children && menu.children.length) {
//             calcMenuNames(menu.name + '/', menu.children)
//         } else if (menu.buttons && menu.buttons.length) {
//             calcMenuNames(menu.name + '/', menu.buttons)
//         }
//     }
// }

const user = {
    state: {
        userInfo: {},
        permission: {},
        allPermission: {},
        roles: [],
        menu: getStore({
            name: 'menu'
        }) || [],
        menuAll: routerData,
        token: getStore({
            name: 'token'
        }) || '',
    },
    actions: {
        //根据用户名登录
        LoginByUsername({
            commit
        }, userInfo) {
            // const user = encryption({
            //     data: userInfo,
            //     type: 'Aes',
            //     key: 'avue',
            //     param: ['useranme', 'password']
            // });
            const user = userInfo;
            return new Promise((resolve) => {
                loginByUsername(user.username, user.password, userInfo.code, userInfo.redomStr).then(data => {
                    commit('SET_TOKEN', data);
                    commit('DEL_ALL_TAG');
                    commit('CLEAR_LOCK');
                    setToken(data);
                    resolve();
                })
            })
        },
        //根据手机号登录
        LoginByPhone({
            commit
        }, userInfo) {
            return new Promise((resolve) => {
                loginByUsername(userInfo.phone, userInfo.code).then(res => {
                    const data = res.data.data;
                    commit('SET_TOKEN', data);
                    commit('DEL_ALL_TAG');
                    commit('CLEAR_LOCK');
                    setToken(data);
                    resolve();
                })
            })
        },
        SetUserInfo({
            commit
        }, userInfo) {
            commit('SET_USERIFNO', userInfo);
        },
        GetUserInfo({
            commit,
        }) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((data) => {
                    commit('SET_USERIFNO', data);
                    // commit('SET_ROLES', data.roles);
                    commit('SET_PERMISSION', data.permissionCodeList || [])
                    resolve(data);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        //刷新token
        RefeshToken({
            commit
        }) {
            return new Promise((resolve, reject) => {
                RefeshToken().then(res => {
                    const data = res.data.data;
                    commit('SET_TOKEN', data);
                    setToken(data);
                    resolve(data);
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        LogOut({
            commit
        }) {
            return new Promise((resolve, reject) => {
                // logout().then(() => {

                // }).catch(error => {
                //     reject(error)
                // })
                commit('SET_TOKEN', '')
                commit('SET_MENU', [])
                commit('SET_ROLES', [])
                commit('DEL_ALL_TAG');
                commit('CLEAR_LOCK');
                removeToken()
                resolve()
            })
        },
        //注销session
        FedLogOut({
            commit
        }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                commit('SET_MENU', [])
                commit('SET_ROLES', [])
                commit('DEL_ALL_TAG');
                commit('CLEAR_LOCK');
                removeToken()
                resolve()
            })
        },
        //获取系统菜单
        GetMenu({
            commit,
            state,
        }) {
            return new Promise(resolve => {
                let menu = deepClone(routerData);
                if (!state.userInfo.superAdmin) {
                    menu = calcMenus(menu, state.permission, state);
                }
                menu.forEach(ele => {
                    addPath(ele, true);
                })
                commit('SET_MENU', menu)
                resolve(menu)
            })
        },
        //获取全部菜单
        GetMenuAll({
            commit
        }) {
            // return new Promise(resolve => {
            //     debugger;
            //     getMenuAll().then((res) => {
            //         const data = res.data.data;
            //         commit('SET_MENU_ALL', data);
            //         resolve(data);
            //     })
            // })
        },

    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
            setStore({
                name: 'token',
                content: state.token,
                type: 'session'
            })
        },
        SET_USERIFNO: (state, userInfo) => {
            state.userInfo = Object.assign(state.userInfo || {}, userInfo);
        },
        SET_MENU: (state, menu) => {
            state.menu = menu
            setStore({
                name: 'menu',
                content: state.menu,
                type: 'session'
            })
        },
        SET_MENU_ALL: (state, menuAll) => {
            state.menuAll = menuAll;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        SET_PERMISSION: (state, permission) => {
            state.permission = {};
            permission.forEach(ele => {
                state.permission[ele] = true;
            });
        }
    }

}
calcMenuNames(routerData);
export default user
